﻿using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using FreeSql.DataAnnotations;
using Model;

namespace DAL
{

    public class Collect
    {
        //新增收藏夹信息到数据库
        public int InsertCollect(CollectInfo collect)
        {
            try
            {
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var col = mongodb.GetCollection<BsonDocument>("CollectList");
                var doc = new BsonDocument();
                doc["account"] = collect.account;
                col.InsertOne(doc);
            }
            catch
            {
                return -1;
            }
            return 0;
        }
        //更新用户的收藏夹
        public int UpdateCollect(CollectInfo collect, string account)
        {
             try
            {
                //考虑当用户修改act的时候，亦需要更改收藏夹的act，认定第二个参数为oldact                
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var col = mongodb.GetCollection<BsonDocument>("CollectList"); //定义获取“Name”值为“xumingxiang”的查询条件
                var query = new QueryDocument { { "account", account } }; //定义更新文档
                var update = new UpdateDocument { { "$set", new QueryDocument { { "account", collect.account }, { "PidList", new BsonArray(collect.PidList) } } } }; //执行更新操作
                col.UpdateOne(query, update);
            }
            catch
            {
                return -1;//更新失败
            }
            return 0;
        }
        //获取用户收藏夹信息
        public CollectInfo GetCollectInfo(string account)
        {
            try
            {
                
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                CollectInfo collect = new CollectInfo(account);
                var query = new QueryDocument { { "account", account } };
                var col = mongodb.GetCollection<BsonDocument>("CollectList");
                var doc = col.Find(query).FirstOrDefault();
                collect.PidList = new List<string>();
                if (doc.Contains("PidList")==true)
                {
                    for (int i = 0; i < doc.GetValue("PidList").AsBsonArray.Count; i++)
                    {
                        string s = doc.GetValue("PidList").AsBsonArray[i].ToString();
                        collect.PidList.Add(s);
                    }
                }
                return collect;
            }
            catch
            {
                return null;
            }

        }
        //删除用户的收藏夹
        public int DeleteCollect(string account)
        {
            try
            {
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var col = mongodb.GetCollection<BsonDocument>("CollectList");
                var query = new QueryDocument { { "account", account } };
                col.DeleteMany(query);
            }
            catch
            {
                return -1;
            }
            return 0;
        }

        public int UpdCollectionAct(string newact, string oldact)
        {
            try
            {
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var col = mongodb.GetCollection<BsonDocument>("CollectList");
                var query = new QueryDocument { { "account", oldact } };
                var update = new UpdateDocument { { "$set", new QueryDocument { { "account", newact } } } }; //执行更新操作
                col.UpdateOne(query, update);
            }
            catch
            {
                return -1;
            }
            return 0;
        }
    }

}
